<template>
  <div class="app-container">
    <el-row :gutter="5">
      <el-col :span="16" :offset="4" :xs="24">
        <el-card class="vacation_edit" v-if="editFlag">
          <div slot="header" class="clearfix">
            <span>请假申请</span>
          </div>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="申请时间：" prop="applyDate">
                  <span>{{ form.applyDate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="申请人：" prop="partyId">
                  <span>{{ partyIdFormat(form) }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="请假类型" prop="vacationTypeId">
                  <el-select v-model="form.vacationTypeId" placeholder="请选择请假类型"
                    @change="selectVacationTypeChange(form.vacationTypeId)">
                    <el-option v-for="dict in vacationTypeIdOptions" :key="dict.enumCode" :label="dict.description"
                      :value="dict.enumCode"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12" v-if="paidVacationFlag">
                <el-form-item label="剩余年假" prop="paidVacation" @change="countVacationTimes($event)">
                  <span>{{ form.paidVacation }} 小时</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="开始时间" prop="fromdate" @change="countVacationTimes($event)">
                  <el-date-picker clearable v-model="form.fromdate" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择开始时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="" prop="fromDateHour">
                  <el-select v-model="form.fromDateHour" placeholder="请选择从几时开始" @change="countVacationTimes($event)">
                    <el-option v-for="dict in fromDateHourOptions" :key="dict.enumCode" :label="dict.description"
                      :value="dict.enumCode"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="结束时间" prop="thrudate" @change="countVacationTimes($event)">
                  <el-date-picker clearable v-model="form.thrudate" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择结束时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="" prop="thruDateHour">
                  <el-select v-model="form.thruDateHour" placeholder="请选择从几时结束" @change="countVacationTimes($event)">
                    <el-option v-for="dict in thruDateHourOptions" :key="dict.enumCode" :label="dict.description"
                      :value="dict.enumCode"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="请假总工时" prop="totalWorkTime">
                  <span>{{ form.totalWorkTime }} 小时</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="20">
                <el-form-item label="请假事由" prop="description">
                  <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="20">
                <el-form-item label="附件" prop="affixFilePath">
                  <image-upload v-model="form.affixFilePath" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="审批人" prop="handler">
                  <LoadUserSelect :placeholder="'请选择审批人'" @getSelectedCourt="getSelectedHandler"></LoadUserSelect>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item size="large" align="center">
                  <el-button type="primary" @click="saveForm">保 存</el-button>
                  <el-button type="primary" @click="submitForm">提 交</el-button>
                  <el-button type="danger" @click="close">关闭</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>

        <el-card class="vacation_detail" v-if="detailFlag">
          <div slot="header" class="clearfix">
            <span>请假申请</span>
            <el-button v-if="applyFlag" style="float: right; padding: 3px 0" type="text" @click="edit">编辑</el-button>
          </div>

          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="申请人：" prop="partyId">
                  <span>{{ partyIdFormat(form) }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="申请时间：" prop="applyDate">
                  <span>{{ form.applyDate }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="请假类型" prop="vacationTypeId">
                  <span>{{ form.vacationTypeId }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="开始时间" prop="fromdate">
                  <span>{{ form.fromdate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="" prop="fromDateHour">
                  <span>{{ form.fromDateHour }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="结束时间：" prop="thrudate">
                  <span>{{ form.thrudate }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="" prop="thruDateHour">
                  <span>{{ form.thruDateHour }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="请假总工时：" prop="totalWorkTime">
                  <span>{{ form.totalWorkTime }} 小时</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="20">
                <el-form-item label="请假事由：" prop="description">
                  <span>{{ form.description }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="20">
                <el-form-item label="附件列表：" prop="affixFilePath">
                  <image-preview :src="form.affixFilePath ? form.affixFilePath : ''" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="审批人：" prop="handler">
                  <span>{{ handlerFormat(form) }}</span>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item size="large" align="center">
                  <el-button type="primary" v-if="editFlag" @click="saveForm">保 存</el-button>
                  <el-button type="primary" @click="submitForm">提 交</el-button>
                  <el-button type="danger" @click="close">关闭</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import LoadUserSelect from "@/components/LoadUserSelect";
import {
  getVacation_record,
  addVacation_record,
  approvalVacation,
  updateVacation_record,
  getEntityDictsForSearch,
  getPaidVacation,
  getWorkingHours,
} from "@/api/ehr/vacation_record";
export default {
  components: { LoadUserSelect },
  name: "apply_vacation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 操作标识,用于区分是编辑还是明细
      editFlag: true,
      // 操作标识,用于区分是明细
      detailFlag: false,
      // 申请人ID业务字典
      partyIdOptions: [],
      // 请假类型业务字典
      vacationTypeIdOptions: [],
      // 从几时开始业务字典
      fromDateHourOptions: [],
      // 从几时结束业务字典
      thruDateHourOptions: [],
      // 审批结论业务字典
      approvalResultOptions: [],
      // 审批人业务字典
      handlerOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,

      paidVacationFlag: false,
      // 表单参数
      form: {},
      formData: {
        mobile: "",
        field101: undefined,
      },
      rules: {
        vacationTypeId: [
          { required: true, message: "请假类型不能为空", trigger: "change" },
        ],
        fromdate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        fromDateHour: [
          { required: true, message: "从几时开始不能为空", trigger: "change" },
        ],
        thrudate: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        thruDateHour: [
          { required: true, message: "从几时结束不能为空", trigger: "change" },
        ],
        approvalResult: [
          { required: true, message: "审批结论不能为空", trigger: "blur" },
        ],
        handler: [
          { required: true, message: "审批人不能为空", trigger: "change" },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.reset();
    // 为申请时间默认赋值
    this.form.applyDate = this.parseTime(new Date(), "{y}-{m}-{d} {h}:{i}:{s}");
    // 为申请人默认值
    this.form.partyId = this.$store.state.user.partyId;
    // 审批人默认值
    // this.form.handler = 1 ;
    this.form.handler = this.$store.state.user.defaultApprover;

    // 员工ID业务字典
    this.getEntityDicts("person:partyId:firstName").then((response) => {
      this.partyIdOptions = response.data;
    });
    // 请假类型业务字典
    this.getEnumDicts("VACATION_TYPE").then((response) => {
      this.vacationTypeIdOptions = response.data;
    });
    // 从几时开始业务字典
    this.getEnumDicts("VACATION_TIME").then((response) => {
      this.fromDateHourOptions = response.data;
    });
    // 从几时结束业务字典
    this.getEnumDicts("VACATION_TIME").then((response) => {
      this.thruDateHourOptions = response.data;
    });
    // 审批结论业务字典
    this.getEnumDicts("APPROVAL_RESULT").then((response) => {
      this.approvalResultOptions = response.data;
    });
    // 审批人业务字典
    this.getEntityDicts("SysUser:userId:userName").then((response) => {
      this.handlerOptions = response.data;
    });
  },

  mounted() { },
  methods: {
    partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    handlerFormat(row, column) {
      let val = null;
      this.handlerOptions.forEach((item, index) => {
        if (row.handler === item.userId + "") {
          val = item.userName;
        }
      });
      return val;
    },
    // 根据partyId 获取 默认审批人
    getDefaultApprover() {
      var partyId = this.$store.state.user.partyId;
      getDefaultApprover(partyId).then((response) => {
        this.form.handler = response.approver;
        console.info("approver:" + this.form.handler);
      });
    },

    // 表单重置
    reset() {
      this.form = {
        paidVacation: null,
        vacationRecordId: null,
        partyId: null,
        applyDate: null,
        vacationTypeId: null,
        fromdate: null,
        fromDateHour: null,
        thrudate: null,
        thruDateHour: null,
        totalWorkTime: null,
        description: null,
        urgentContact: null,
        tempPartyId: null,
        marriedDate: null,
        childrenDate: null,
        deathDate: null,
        deathRelative: null,
        inCompanyFlg: null,
        statusId: null,
        affixFilePath: null,
        attendanceRuleId: null,
        relationid: null,
        approvalResult: null,
        approvalComments: null,
        flowInstId: null,
        flowStatus: null,
        handler: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
      };
      //this.resetForm("form");
    },
    // 审批人选择赋值
    getSelectedHandler(e) {
      this.form.handler = e;
    },
    // 用户剩余年假
    selectVacationTypeChange(vacationTypeId) {
      console.info("vacationTypeId:" + vacationTypeId);
      // 若是 年假 则显示 剩余年假
      if (vacationTypeId == "paid") {
        this.paidVacationFlag = true;
        getPaidVacation(this.form.partyId).then((response) => {
          this.form.paidVacation = response.paidVacation;
          console.info("paidVacation:" + this.form.paidVacation);
        });
      } else {
        this.paidVacationFlag = false;
      }
    },
    countVacationTimes(value) {
      console.info("paidVacation:" + value);
      var startDate = this.form.fromdate;
      var startTime = this.form.fromDateHour;
      var endDate = this.form.thrudate;
      var endTime = this.form.thruDateHour;
      console.info("startDate:" + startDate);
      console.info("startTime:" + startTime);
      console.info("endDate:" + endDate);
      console.info("endTime:" + endTime);

      if (startDate && startTime && endDate && endTime) {
        getWorkingHours(
          startDate,
          startTime + ":00",
          endDate,
          endTime + ":00"
        ).then((response) => {
          this.form.totalWorkTime = response.workHours;
          console.info("workHours:" + this.form.totalWorkTime);
        });
      }
    },
    /** 保存按钮 */
    saveForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.vacationRecordId != null && this.updateFlag) {
            updateVacation_record(this.form).then((response) => {
              this.$modal.msgSuccess("修改请假申请成功");
              this.open = false;
            });
          } else {
            addVacation_record(this.form).then((response) => {
              this.form.vacationRecordId = response.data.vacationRecordId;
              console.info("vacationID" + this.form.vacationRecordId);
              this.$modal.msgSuccess("新增请假申请成功");
              this.open = false;
            });
          }
          this.loading = false;

          this.editFlag = false;
          this.detailFlag = true;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          approvalVacation(this.form).then((response) => {
            this.$modal.msgSuccess("提交请假申请成功");
            this.loading = false;
            this.open = false;
          });
          this.close();
        }
      });
    },
    /** 编辑 */
    edit() {
      this.editFlag = true;
      this.detailFlag = false;
    },
    resetForm() {
      this.$refs["form"].resetFields();
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
